﻿using System;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;

using Npgsql;
using CoreLab.Common;
using CoreLab.PostgreSql;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2007-12-21	
	Status:		alpha	
	Version:	2.0.0
	Build:		20080507
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-------------------------------------------------------------------------------------------------
	Note re EngineX.DataX:
	------------------------------------
	This component is intended as a data layer to abstract the use of any particular data connector.
	The original source for this was taken from the following url:
		http://aspalliance.com/837
	-------------------------------------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------
	Development Notes:
	==================
	20071221:	Starting date is a pallendrome!
	20071221:	Extend for PostGreSQL
	20080507:	Added support for PostGreSQL using CoreLab's data provider
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.dataX {
	/// <summary>
	/// 
	/// </summary>
	public sealed class DataXManagerFactory {
		#region Visible properties
		#endregion

		#region Constructors/Destructors
		/// <summary>
		/// Default constructor
		/// </summary>
		private DataXManagerFactory(){}
		#endregion

		#region Public methods
		/// <summary>
		/// Gets a connection to the data provider
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <returns></returns>
		public static IDbConnection GetConnection(DataProviderType providerType) {
			IDbConnection iDbConnection = null;
			switch (providerType) {
				case DataProviderType.SqlServer:
					iDbConnection = new SqlConnection();
					break;
				case DataProviderType.Npgsql:
					iDbConnection = new NpgsqlConnection();
					break;
				case DataProviderType.PostgreSQLDirect:
					iDbConnection = new PgSqlConnection();
					break;
				case DataProviderType.OleDb:
					iDbConnection = new OleDbConnection();
					break;
				case DataProviderType.Odbc:
					iDbConnection = new OdbcConnection();
					break;
				case DataProviderType.Oracle:
					iDbConnection = new OracleConnection();
					break;
				default:
					return null;
			}
			return iDbConnection;
		}

		/// <summary>
		/// Gets a connection to the data provider using a connection string
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <param name="connect">The connection string to the data provider</param>
		/// <returns></returns>
		public static IDbConnection GetConnection(DataProviderType providerType, string connect) {
			IDbConnection iDbConnection = null;
			switch (providerType) {
				case DataProviderType.SqlServer:
					iDbConnection = new SqlConnection(connect);
					break;
				case DataProviderType.Npgsql:
					iDbConnection = new NpgsqlConnection(connect);
					break;
				case DataProviderType.PostgreSQLDirect:
					iDbConnection = new PgSqlConnection(connect);
					break;
				case DataProviderType.OleDb:
					iDbConnection = new OleDbConnection(connect);
					break;
				case DataProviderType.Odbc:
					iDbConnection = new OdbcConnection(connect);
					break;
				case DataProviderType.Oracle:
					iDbConnection = new OracleConnection(connect);
					break;
				default:
					return null;
			}
			return iDbConnection;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <returns></returns>
		public static IDbCommand GetCommand(DataProviderType providerType) {
			switch (providerType) {
				case DataProviderType.SqlServer:
					return new SqlCommand();
				case DataProviderType.Npgsql:
					return new NpgsqlCommand();
				case DataProviderType.PostgreSQLDirect:
					return new PgSqlCommand();
				case DataProviderType.OleDb:
					return new OleDbCommand();
				case DataProviderType.Odbc:
					return new OdbcCommand();
				case DataProviderType.Oracle:
					return new OracleCommand();
				default:
					return null;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <returns></returns>
		public static IDbDataAdapter GetDataAdapter(DataProviderType providerType) {
			switch (providerType) {
				case DataProviderType.SqlServer:
					return new SqlDataAdapter();
				case DataProviderType.Npgsql:
					return new NpgsqlDataAdapter();
				case DataProviderType.PostgreSQLDirect:
					return new PgSqlDataAdapter();
				case DataProviderType.OleDb:
					return new OleDbDataAdapter();
				case DataProviderType.Odbc:
					return new OdbcDataAdapter();
				case DataProviderType.Oracle:
					return new OracleDataAdapter();
				default:
					return null;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <returns></returns>
		public static IDbTransaction GetTransaction(DataProviderType providerType) {
			IDbConnection iDbConnection = GetConnection(providerType);
			IDbTransaction iDbTransaction = iDbConnection.BeginTransaction();
			return iDbTransaction;
		}

		/// <overloads>Gets a new parameter object</overloads>
		/// <summary>
		/// Gets a new, uninitialised parameter object
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <returns></returns>
		public static IDataParameter GetParameter(DataProviderType providerType) {
			IDataParameter iDataParameter = null;
			switch (providerType) {
				case DataProviderType.SqlServer:
					iDataParameter = new SqlParameter();
					break;
				case DataProviderType.Npgsql:
					iDataParameter = new NpgsqlParameter();
					break;
				case DataProviderType.PostgreSQLDirect:
					iDataParameter = new PgSqlParameter();
					break;
				case DataProviderType.OleDb:
					iDataParameter = new OleDbParameter();
					break;
				case DataProviderType.Odbc:
					iDataParameter = new OdbcParameter();
					break;
				case DataProviderType.Oracle:
					iDataParameter = new OracleParameter();
					break;
			}
			return iDataParameter;
		}

		/// <summary>
		/// Gets a new parameter object and initialises the name and type
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <param name="paramName">The parameter name</param>
		/// <param name="paramType">The type of the parameter</param>
		/// <returns></returns>
		public static IDataParameter GetParameter(DataProviderType providerType, string paramName, DbType paramType) {
			IDataParameter iDataParameter = null;
			switch (providerType) {
				case DataProviderType.SqlServer:
					iDataParameter = new SqlParameter(paramName, paramType);
					break;
				case DataProviderType.Npgsql:
					iDataParameter = new NpgsqlParameter(paramName, paramType);
					break;
				case DataProviderType.PostgreSQLDirect:
					iDataParameter = new PgSqlParameter(paramName, paramType);
					break;
				case DataProviderType.OleDb:
					iDataParameter = new OleDbParameter(paramName, (OleDbType)paramType);
					break;
				case DataProviderType.Odbc:
					iDataParameter = new OdbcParameter(paramName, (OdbcType)paramType);
					break;
				case DataProviderType.Oracle:
					iDataParameter = new OracleParameter(paramName, (OracleType)paramType);
					break;
			}
			return iDataParameter;
		}

		/// <summary>
		/// Gets a new parameter object and initialises the name, type and size
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <param name="paramName">The parameter name</param>
		/// <param name="paramType">The type of the parameter</param>
		/// <param name="paramSize">The size of the parameter</param>
		/// <returns></returns>
		public static IDataParameter GetParameter(DataProviderType providerType, string paramName, DbType paramType, int paramSize) {
			IDataParameter iDataParameter = null;
			switch (providerType) {
				case DataProviderType.SqlServer:
					iDataParameter = new SqlParameter(paramName, paramType);
					((SqlParameter)iDataParameter).Size = paramSize;	// NB: Doesn't like the constructor with size??
					break;
				case DataProviderType.Npgsql:
					iDataParameter = new NpgsqlParameter(paramName, paramType , paramSize);
					break;
				case DataProviderType.PostgreSQLDirect:
					iDataParameter = new PgSqlParameter(paramName, (PgSqlType)paramType, paramSize);
					break;
				case DataProviderType.OleDb:
					iDataParameter = new OleDbParameter(paramName, (OleDbType)paramType, paramSize);
					break;
				case DataProviderType.Odbc:
					iDataParameter = new OdbcParameter(paramName, (OdbcType)paramType, paramSize);
					break;
				case DataProviderType.Oracle:
					iDataParameter = new OracleParameter(paramName, (OracleType)paramType, paramSize);
					break;
			}
			return iDataParameter;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="providerType">Type of data provider</param>
		/// <param name="paramsCount"></param>
		/// <returns></returns>
		public static IDbDataParameter[] GetParameters(DataProviderType providerType, int paramsCount) {
			IDbDataParameter[]idbParams = new IDbDataParameter[paramsCount];
			switch (providerType) {
				case DataProviderType.SqlServer:
					for (int i = 0; i < paramsCount;++i) {
						idbParams[i] = new SqlParameter();
					}
					break;
				case DataProviderType.Npgsql:
					for (int i = 0; i < paramsCount; ++i) {
						idbParams[i] = new NpgsqlParameter();
					}
					break;
				case DataProviderType.PostgreSQLDirect:
					for (int i = 0; i < paramsCount; ++i) {
						idbParams[i] = new PgSqlParameter();
					}
					break;
				case DataProviderType.OleDb:
					for (int i = 0; i < paramsCount;++i) {
						idbParams[i] = new OleDbParameter();
					}
					break;
				case DataProviderType.Odbc:
					for (int i = 0; i < paramsCount;++i) {
						idbParams[i] = new OdbcParameter();
					}
					break;
				case DataProviderType.Oracle:
					for (int i = 0; i < paramsCount; ++i) {
						idbParams[i] = new OracleParameter();
					}
					break;
				default:
					idbParams = null;
					break;
				}
			return idbParams;
		}
		#endregion

		#region Protected methods
		#endregion

		#region Private methods
		#endregion
	}
}